import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { UserInput } from './dto/user-input.type';
import { UserService } from './user.service';
import { UserType } from './dto/user.type';
import { UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from '../../guards/auth.guard';
import { CurUserId } from '../../decorators/current-user.decorator';
import { Result } from '../../dto/result.type';

@Resolver()
export class UserResolver {
  constructor(private readonly userService: UserService) {}

  @Mutation(() => Result, { description: '创建新用户' })
  async createUser(@Args('params') params: UserInput): Promise<Result> {
    return await this.userService.createUser(params);
  }

  @UseGuards(GqlAuthGuard)
  @Query(() => UserType, { description: '使用 ID 查询用户' })
  async findUserById(@Args('id') id: string): Promise<UserType> {
    return await this.userService.findUserById(id);
  }

  @UseGuards(GqlAuthGuard)
  @Query(() => UserType, { description: '使用登录后获取到的 ID 查询用户' })
  async getUserInfo(@CurUserId() userId: string): Promise<UserType> {
    return await this.userService.findUserById(userId);
  }

  @UseGuards(GqlAuthGuard)
  @Mutation(() => Result, { description: '更新用户简介和昵称' })
  async updateUser(
    @Args('id') id: string,
    @Args('params') params: UserInput,
  ): Promise<Result> {
    return await this.userService.updateUser(id, params);
  }

  @UseGuards(GqlAuthGuard)
  @Mutation(() => Boolean, { description: '删除一个用户' })
  async del(@Args('id') id: string): Promise<boolean> {
    return await this.userService.delUser(id);
  }
}
